package cn.tedu.ems.pojo.vo;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

@Data
@TableName("manager")
@Slf4j
public class UserVO implements Serializable {
    @ApiModelProperty(value = "用户id")
    @TableId(type = IdType.AUTO)
    Integer id;
    @ApiModelProperty(value = "用户名")
    String username;

    @ApiModelProperty(value = "密码")
    @JsonIgnore
    String password;

    @ApiModelProperty(value = "状态")
    String status;

    @ApiModelProperty(value = "token令牌")
    String token;

    /**
     * 用于把jwt token储存的Json重新转位对象
     *
     * @param json
     * @return
     */
    public static UserVO formJson(String json) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json, UserVO.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 把UserVO变为Json,用于储存在jwt token中
     *
     * @return
     */
    public String toJson() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
